package org.zkdn.modules.wlpt.basicinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.zkdn.modules.wlpt.basicinfo.entity.*;
import org.zkdn.modules.wlpt.basicinfo.enums.DictCodeEnums;
import org.zkdn.modules.wlpt.basicinfo.mapper.TRoadMapper;
import org.zkdn.modules.wlpt.basicinfo.service.ITRoadRefCrossService;
import org.zkdn.modules.wlpt.basicinfo.service.ITRoadRefTagService;
import org.zkdn.modules.wlpt.basicinfo.service.ITRoadService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.zkdn.modules.wlpt.basicinfo.vo.RoadVO;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Description: t_road
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Service
public class TRoadServiceImpl extends ServiceImpl<TRoadMapper, TRoad> implements ITRoadService {

    @Autowired
    private TRoadMapper roadMapper;

    @Autowired
    private ITRoadRefTagService roadRefTagService;

    @Autowired
    private ITRoadRefCrossService roadRefCrossService;

    @Override
    public IPage<RoadVO> queryPageList(IPage<RoadVO> page, QueryWrapper queryWrapper) {
        return roadMapper.queryPageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveByValidate(TRoad road) {
        QueryWrapper<TRoad> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", road.getCode());
        List<TRoad> exitsList = roadMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(exitsList)) {
            return Result.error("道路编号'" + road.getCode() + "'已存在!" );
        }
        roadMapper.insert(road);

        String tags = road.getRoadTag();
        List<String> tagList = Arrays.asList(tags.split(","));
        if (CollectionUtils.isNotEmpty(tagList)) {
            // 创建TRoadRefTag集合
            List<TRoadRefTag> roadRefTagList = tagList.stream()
                    .map(tag -> {
                        TRoadRefTag roadRefTag = new TRoadRefTag();
                        roadRefTag.setDictItemValue(tag);
                        roadRefTag.setRoadId(road.getId());
                        roadRefTag.setDictCode(DictCodeEnums.ROAD_TAG.getDictCode());
                        return roadRefTag;
                    })
                    .collect(Collectors.toList());

            // 输出结果
            roadRefTagList.forEach(System.out::println);
            roadRefTagService.saveBatch(roadRefTagList);
        }
        return Result.OK("添加成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateByValidate(TRoad road) {
        QueryWrapper<TRoad> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", road.getCode());
        queryWrapper.ne("id", road.getId());
        List<TRoad> exitsList = roadMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(exitsList)) {
            return Result.error("道路编号'" + road.getCode() + "'已存在!" );
        }
        roadMapper.updateById(road);

        String tags = road.getRoadTag();
        List<String> tagList = Arrays.asList(tags.split(","));
        if (CollectionUtils.isNotEmpty(tagList)) {
            //先删除
            roadRefTagService.remove(new QueryWrapper<TRoadRefTag>().eq("road_id", road.getId()));

            // 创建TRoadRefTag集合
            List<TRoadRefTag> roadRefTagList = tagList.stream()
                    .map(tag -> {
                        TRoadRefTag roadRefTag = new TRoadRefTag();
                        roadRefTag.setDictItemValue(tag);
                        roadRefTag.setRoadId(road.getId());
                        roadRefTag.setDictCode(DictCodeEnums.ROAD_TAG.getDictCode());
                        return roadRefTag;
                    })
                    .collect(Collectors.toList());

            roadRefTagService.saveBatch(roadRefTagList);
        }
        return Result.OK("修改成功！");
    }

    @Override
    public Result deleteById(String id) {
        QueryWrapper<TRoadRefCross> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("road_id", id);
        roadRefCrossService.remove(queryWrapper);
        roadMapper.deleteById(id);
        return Result.OK("删除成功！");
    }

    @Override
    public Result deleteByIds(String ids) {
        List<String> roadIdList = Arrays.asList(ids.split(","));
        if (CollectionUtils.isNotEmpty(roadIdList)) {
            roadIdList.stream().forEach(roadId -> {
                QueryWrapper<TRoadRefCross> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("road_id", roadId);
                roadRefCrossService.remove(queryWrapper);
            });
            roadMapper.deleteBatchIds(roadIdList);
        }
        return Result.OK("批量删除成功!");
    }
}
